package com.example.module_main.loading

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.example.lib_storage.mmkv.FirstMMKV
import com.example.module_main.MainActivity
import com.example.module_main.R
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator


/*
* 引导页
* */
class LoadingActivity : AppCompatActivity() {

    private lateinit var vip2: ViewPager2
    private lateinit var but: Button
    private lateinit var tabIndicator: TabLayout
    private lateinit var skip: TextView
    private var firstEnter: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_loading)
        firstEnter = FirstMMKV.mmkv.decodeBool("first")
        if (firstEnter){
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }
        //权限
        getPermissions()
        //小控件
        initItem()

        //跳过点击事件
        skip.setOnClickListener {
            startActivity(Intent(this,MainActivity::class.java))
            FirstMMKV.mmkv.encode("first",true)
            finish()
        }


        //vip2的绑定
        vip2.adapter = Vip2Adapter()
        vip2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position == 2){
                    but.visibility = View.VISIBLE
                }else{
                    but.visibility = View.GONE
                }
            }
        })

        //tabLayout的绑定
        TabLayoutMediator(tabIndicator, vip2) { _, _ -> }.attach()

        //立即体验点击事件
        but.setOnClickListener {
            startActivity(Intent(this,MainActivity::class.java))
            FirstMMKV.mmkv.encode("first",true)
            finish()
        }

    }

    fun getPermissions(){
        requestPermissions(
            listOf(
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
            ).toTypedArray(),101)
    }

    fun initItem(){
        vip2 = findViewById(R.id.vip2)
        but = findViewById(R.id.btn_start)
        tabIndicator = findViewById(R.id.tab_indicator)
        skip = findViewById(R.id.steer_skip)
    }
}